#!/usr/bin/env bash
# Summary: Set or show the shell-specific sbt version
#
# Usage: sbtenv shell <version>
#        sbtenv shell --unset

set -e
test -n "${SBTENV_DEBUG}" && set -x

if [ "${1}" = "--complete" ]; then
  echo --unset
  echo system
  exec sbtenv-versions --base
fi

version="${1}"
shell="$(basename "${SBTENV_SHELL:-${SHELL}}")"

if [ -z "${version}" ]; then
  if [ -z "${SBTENV_VERSION}" ]; then
    echo "sbtenv: no shell-specific version configured" >& 2
    exit 1
  else
    echo "echo \"\${SBTENV_VERSION}\""
    exit 0
  fi
fi

if [ "${version}" = "--unset" ]; then
  case "${shell}" in
    * )
      echo "unset SBTENV_VERSION"
      ;;
  esac
  exit 0
fi

if sbtenv-prefix "${version}" > /dev/null; then
  case "${shell}" in
    * )
      echo "export SBTENV_VERSION=\"${version}\""
      ;;
  esac
  exit 0
else
  echo "false"
  exit 1
fi

